const fs = require('fs').promises;
const path = require('path');
const zlib = require('zlib');
const util = require('util');

class BPlusTrees {
    constructor(order = 4) {
        this.root = new Node(true);
        this.order = order;
    }

    insert(key, value) {
        if (this.root.keys.length === 0) {
            this.root.keys.push(key);
            this.root.values.push([value]);
            return;
        }

        const node = this.findLeaf(key);
        const pos = this.findPosition(node, key);
        
        if (pos < node.keys.length && node.keys[pos] === key) {
            node.values[pos].push(value);
        } else {
            node.keys.splice(pos, 0, key);
            node.values.splice(pos, 0, [value]);
            
            if (node.keys.length >= this.order) {
                this.split(node);
            }
        }
    }

    search(key) {
        const node = this.findLeaf(key);
        const pos = this.findPosition(node, key);
        
        if (pos < node.keys.length && node.keys[pos] === key) {
            return node.values[pos];
        }
        return [];
    }

    remove(key) {
        const node = this.findLeaf(key);
        const pos = this.findPosition(node, key);
        
        if (pos < node.keys.length && node.keys[pos] === key) {
            node.keys.splice(pos, 1);
            node.values.splice(pos, 1);
            this.rebalance(node);
        }
    }

    findLeaf(key) {
        let node = this.root;
        while (!node.isLeaf) {
            const pos = this.findPosition(node, key);
            node = node.children[pos];
        }
        return node;
    }

    findPosition(node, key) {
        let left = 0;
        let right = node.keys.length;
        
        while (left < right) {
            const mid = Math.floor((left + right) / 2);
            if (node.keys[mid] === key) {
                return mid;
            } else if (node.keys[mid] < key) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        return left;
    }

    split(node) {
        const mid = Math.floor(node.keys.length / 2);
        const rightNode = new Node(node.isLeaf);
        rightNode.keys = node.keys.splice(mid);
        rightNode.values = node.values.splice(mid);
        
        if (!node.isLeaf) {
            rightNode.children = node.children.splice(mid);
        }
        
        if (node === this.root) {
            const newRoot = new Node(false);
            newRoot.keys = [rightNode.keys[0]];
            newRoot.children = [node, rightNode];
            this.root = newRoot;
        } else {
            const parent = this.findParent(node);
            const pos = this.findPosition(parent, rightNode.keys[0]);
            parent.keys.splice(pos, 0, rightNode.keys[0]);
            parent.children.splice(pos + 1, 0, rightNode);
            
            if (parent.keys.length >= this.order) {
                this.split(parent);
            }
        }
    }

    rebalance(node) {
        if (node === this.root) {
            if (node.keys.length === 0 && node.children.length > 0) {
                this.root = node.children[0];
            }
            return;
        }

        const minKeys = Math.ceil(this.order / 2) - 1;
        if (node.keys.length >= minKeys) return;

        const parent = this.findParent(node);
        const pos = parent.children.indexOf(node);
        
        if (pos > 0) {
            const leftSibling = parent.children[pos - 1];
            if (leftSibling.keys.length > minKeys) {
                node.keys.unshift(leftSibling.keys.pop());
                node.values.unshift(leftSibling.values.pop());
                parent.keys[pos - 1] = node.keys[0];
                return;
            }
        }
        
        if (pos < parent.children.length - 1) {
            const rightSibling = parent.children[pos + 1];
            if (rightSibling.keys.length > minKeys) {
                node.keys.push(rightSibling.keys.shift());
                node.values.push(rightSibling.values.shift());
                parent.keys[pos] = rightSibling.keys[0];
                return;
            }
        }
        
        if (pos > 0) {
            this.merge(parent.children[pos - 1], node, parent, pos - 1);
        } else {
            this.merge(node, parent.children[pos + 1], parent, pos);
        }
    }

    merge(leftNode, rightNode, parent, pos) {
        leftNode.keys = leftNode.keys.concat(rightNode.keys);
        leftNode.values = leftNode.values.concat(rightNode.values);
        
        if (!leftNode.isLeaf) {
            leftNode.children = leftNode.children.concat(rightNode.children);
        }
        
        parent.keys.splice(pos, 1);
        parent.children.splice(pos + 1, 1);
        
        if (parent === this.root && parent.keys.length === 0) {
            this.root = leftNode;
        } else if (parent.keys.length < Math.ceil(this.order / 2) - 1) {
            this.rebalance(parent);
        }
    }

    findParent(node, current = this.root, parent = null) {
        if (current === node) return parent;
        if (current.isLeaf) return null;
        
        for (let i = 0; i < current.children.length; i++) {
            const result = this.findParent(node, current.children[i], current);
            if (result) return result;
        }
        return null;
    }
}

class Node {
    constructor(isLeaf = false) {
        this.isLeaf = isLeaf;
        this.keys = [];
        this.values = [];
        this.children = isLeaf ? null : [];
        this.next = null;
    }
}
const BPlusTree = new BPlusTrees;
const compress = util.promisify(zlib.gzip);
const decompress = util.promisify(zlib.gunzip);

class DeadlockDetector {
    constructor() {
        this.waitForGraph = new Map();
    }

    addWaitsFor(transaction1, transaction2) {
        if (!this.waitForGraph.has(transaction1)) {
            this.waitForGraph.set(transaction1, new Set());
        }
        this.waitForGraph.get(transaction1).add(transaction2);
        return this.detectCycle(transaction1);
    }

    detectCycle(startNode, visited = new Set()) {
        if (visited.has(startNode)) {
            return true;
        }
        visited.add(startNode);
        const edges = this.waitForGraph.get(startNode);
        if (edges) {
            for (const node of edges) {
                if (this.detectCycle(node, visited)) {
                    return true;
                }
            }
        }
        visited.delete(startNode);
        return false;
    }

    removeTransaction(transactionId) {
        this.waitForGraph.delete(transactionId);
        for (const [_, waitSet] of this.waitForGraph) {
            waitSet.delete(transactionId);
        }
    }
}

class LockManager {
    constructor() {
        this.locks = new Map();
        this.waitQueue = new Map();
        this.deadlockDetector = new DeadlockDetector();
    }

    async acquireLock(resource, transactionId, mode = 'READ') {
        const lock = this.locks.get(resource) || { readers: new Set(), writer: null };
        
        if (lock.writer && mode === 'WRITE') {
            const hasDeadlock = this.deadlockDetector.addWaitsFor(transactionId, lock.writer);
            if (hasDeadlock) {
                throw new Error('检测到死锁，事务已回滚');
            }
        }
        
        if (mode === 'READ' && !lock.writer) {
            lock.readers.add(transactionId);
            this.locks.set(resource, lock);
            return true;
        }
        
        if (mode === 'WRITE' && !lock.writer && lock.readers.size === 0) {
            lock.writer = transactionId;
            this.locks.set(resource, lock);
            return true;
        }

        return new Promise((resolve) => {
            const queue = this.waitQueue.get(resource) || [];
            queue.push({ transactionId, mode, resolve });
            this.waitQueue.set(resource, queue);
        });
    }

    releaseLock(resource, transactionId) {
        const lock = this.locks.get(resource);
        if (!lock) return;

        if (lock.writer === transactionId) {
            lock.writer = null;
        }
        lock.readers.delete(transactionId);

        this.processWaitQueue(resource);
        this.deadlockDetector.removeTransaction(transactionId);
    }

    processWaitQueue(resource) {
        const queue = this.waitQueue.get(resource) || [];
        const lock = this.locks.get(resource);
        
        while (queue.length > 0) {
            const { transactionId, mode, resolve } = queue[0];
            
            if (mode === 'READ' && !lock.writer) {
                lock.readers.add(transactionId);
                queue.shift();
                resolve(true);
            } else if (mode === 'WRITE' && !lock.writer && lock.readers.size === 0) {
                lock.writer = transactionId;
                queue.shift();
                resolve(true);
            } else {
                break;
            }
        }

        if (queue.length === 0) {
            this.waitQueue.delete(resource);
        }
    }
}

class MVCCManager {
    constructor() {
        this.versions = new Map();
        this.transactionTimestamps = new Map();
        this.nextTimestamp = 1;
    }

    getTimestamp() {
        return this.nextTimestamp++;
    }

    createVersion(tableName, rowId, value, transactionId) {
        const key = `${tableName}:${rowId}`;
        if (!this.versions.has(key)) {
            this.versions.set(key, []);
        }
        const versions = this.versions.get(key);
        versions.push({
            value,
            startTs: this.transactionTimestamps.get(transactionId),
            endTs: Number.MAX_SAFE_INTEGER
        });
    }

    getVersion(tableName, rowId, transactionId) {
        const key = `${tableName}:${rowId}`;
        const versions = this.versions.get(key) || [];
        const ts = this.transactionTimestamps.get(transactionId);
        
        return versions.find(v => v.startTs <= ts && v.endTs > ts)?.value;
    }
}

class VectorizedProcessor {
    constructor() {
        this.operations = {
            sum: (column) => column.reduce((a, b) => a + b, 0),
            avg: (column) => column.reduce((a, b) => a + b, 0) / column.length,
            min: (column) => Math.min(...column),
            max: (column) => Math.max(...column),
            count: (column, options = {}) => {
                if (options.distinct) {
                    return new Set(column.filter(v => v != null)).size;
                } else if (column === '*') {
                    return options.data ? options.data.length : 0;
                } else {
                    return column.filter(v => v != null).length;
                }
            },
            distinct: (column) => new Set(column).size
        };
    }

    compute(operation, column, options = {}) {
        if (this.operations[operation]) {
            return this.operations[operation](column, options);
        }
        throw new Error(`不支持的操作: ${operation}`);
    }

    filter(column, predicate) {
        return column.filter(predicate);
    }

    transform(column, transformer) {
        return column.map(transformer);
    }
}

class QueryOptimizer {
    constructor(db) {
        this.db = db;
        this.statistics = new Map();
    }

    async collectStatistics(tableName) {
        const table = this.db.tables.get(tableName);
        if (!table) return;

        const stats = {
            rowCount: table.data.length,
            columnStats: new Map(),
            indexStats: new Map()
        };

        for (const [column, type] of Object.entries(table.schema)) {
            const values = table.data.map(row => row[column]);
            stats.columnStats.set(column, {
                distinctCount: new Set(values).size,
                nullCount: values.filter(v => v === null).length,
                min: type === 'number' ? Math.min(...values) : undefined,
                max: type === 'number' ? Math.max(...values) : undefined
            });
        }

        for (const indexName of table.indexes) {
            const index = this.db.indexes.get(`${tableName}:${indexName}`);
            if (index) {
                stats.indexStats.set(indexName, {
                    type: index.type,
                    depth: index.tree.getDepth(),
                    nodeCount: index.tree.getNodeCount()
                });
            }
        }

        this.statistics.set(tableName, stats);
    }

    selectBestIndex(tableName, conditions) {
        const stats = this.statistics.get(tableName);
        if (!stats) return null;

        const table = this.db.tables.get(tableName);
        const availableIndexes = Array.from(table.indexes);
        
        let bestIndex = null;
        let bestCost = Infinity;

        for (const indexName of availableIndexes) {
            const indexStats = stats.indexStats.get(indexName);
            if (!indexStats) continue;

            const cost = this.estimateIndexCost(indexStats, conditions);
            if (cost < bestCost) {
                bestCost = cost;
                bestIndex = indexName;
            }
        }

        return bestIndex;
    }

    estimateIndexCost(indexStats, conditions) {
        const { depth, nodeCount } = indexStats;
        const selectivity = this.estimateSelectivity(conditions);
        return depth * Math.log2(nodeCount) * selectivity;
    }

    estimateSelectivity(conditions) {
        return Object.keys(conditions).length * 0.1;
    }

    optimizeQuery(query) {
        const { tableName, where, columns } = query;
        const stats = this.statistics.get(tableName);
        if (!stats) return query;

        const optimizedQuery = { ...query };

        if (where) {
            const bestIndex = this.selectBestIndex(tableName, where);
            if (bestIndex) {
                optimizedQuery.useIndex = bestIndex;
            }
        }

        if (columns) {
            const table = this.db.tables.get(tableName);
            if (table.storageType === 'column') {
                optimizedQuery.useColumnStore = true;
            }
        }

        return optimizedQuery;
    }
}

class TransactionManager {
    constructor() {
        this.ISOLATION_LEVELS = {
            READ_UNCOMMITTED: 'READ_UNCOMMITTED',
            READ_COMMITTED: 'READ_COMMITTED',
            REPEATABLE_READ: 'REPEATABLE_READ',
            SERIALIZABLE: 'SERIALIZABLE'
        };
        this.defaultIsolationLevel = this.ISOLATION_LEVELS.READ_COMMITTED;
    }

    isVisible(version, transaction) {
        switch (transaction.isolationLevel) {
            case this.ISOLATION_LEVELS.READ_UNCOMMITTED:
                return true;
            case this.ISOLATION_LEVELS.READ_COMMITTED:
                return version.committed || version.transactionId === transaction.id;
            case this.ISOLATION_LEVELS.REPEATABLE_READ:
            case this.ISOLATION_LEVELS.SERIALIZABLE:
                return version.timestamp <= transaction.timestamp;
            default:
                return false;
        }
    }
}

class RowLockManager {
    constructor() {
        this.locks = new Map();
    }

    async acquireLock(tableName, rowId, transactionId, mode = 'READ') {
        const key = `${tableName}:${rowId}`;
        const lock = this.locks.get(key) || { readers: new Set(), writer: null };

        if (mode === 'READ') {
            if (lock.writer && lock.writer !== transactionId) {
                throw new Error('行已被其他事务写锁定');
            }
            lock.readers.add(transactionId);
        } else {
            if (lock.writer || lock.readers.size > 0) {
                throw new Error('行已被锁定');
            }
            lock.writer = transactionId;
        }

        this.locks.set(key, lock);
    }

    releaseLock(tableName, rowId, transactionId) {
        const key = `${tableName}:${rowId}`;
        const lock = this.locks.get(key);
        if (!lock) return;

        if (lock.writer === transactionId) {
            lock.writer = null;
        }
        lock.readers.delete(transactionId);

        if (!lock.writer && lock.readers.size === 0) {
            this.locks.delete(key);
        }
    }
}

class OptimisticLockManager {
    constructor() {
        this.versions = new Map();
    }

    getVersion(tableName, rowId) {
        const key = `${tableName}:${rowId}`;
        return this.versions.get(key) || 0;
    }

    incrementVersion(tableName, rowId) {
        const key = `${tableName}:${rowId}`;
        const version = (this.versions.get(key) || 0) + 1;
        this.versions.set(key, version);
        return version;
    }

    validateVersion(tableName, rowId, expectedVersion) {
        const currentVersion = this.getVersion(tableName, rowId);
        return currentVersion === expectedVersion;
    }
}

class BackupManager {
    constructor(db) {
        this.db = db;
        this.backupPath = path.join(db.config.storage.path, 'backups');
        this.backupInterval = null;
    }

    startAutoBackup() {
        if (this.db.config.backup.enabled) {
            this.backupInterval = setInterval(() => {
                this.createBackup().catch(console.error);
            }, this.db.config.backup.interval);
        }
    }

    stopAutoBackup() {
        if (this.backupInterval) {
            clearInterval(this.backupInterval);
            this.backupInterval = null;
        }
    }

    async createBackup() {
        const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
        const backupDir = path.join(this.backupPath, timestamp);
        
        await this.db.logger.info('开始创建备份', { timestamp });
        
        try {
            // 创建备份目录
            await fs.mkdir(backupDir, { recursive: true });

            // 备份元数据
            await fs.writeFile(
                path.join(backupDir, 'metadata.json'),
                JSON.stringify(this.db.data.metadata, null, 2)
            );

            // 备份所有在元数据中定义的表
            for (const [tableName, tableInfo] of Object.entries(this.db.data.metadata.tables)) {
                try {
                    // 尝试从内存中获取表数据
                    let table = this.db.tables.get(tableName);
                    
                    // 如果表不在内存中，尝试从文件加载
                    if (!table) {
                        try {
                            table = await this.db.读取表文件(tableName);
                        } catch (error) {
                            console.error(`无法读取表 ${tableName} 的数据:`, error);
                            continue;
                        }
                    }

                    if (table) {
                        // 压缩并保存表数据
                        const tableContent = JSON.stringify(table, null, 2);
                        const compressedContent = await compress(tableContent);
                        await fs.writeFile(
                            path.join(backupDir, `${tableName}.json`),
                            compressedContent
                        );
                    }
                } catch (error) {
                    console.error(`备份表 ${tableName} 失败:`, error);
                }
            }

            // 更新最后备份时间
            this.db.data.metadata.lastBackup = timestamp;
            await this.db.保存元数据();

            await this.db.logger.info('备份创建完成', {
                path: backupDir,
                tablesCount: Object.keys(this.db.data.metadata.tables).length
            });

            return {
                success: true,
                backupPath: backupDir,
                timestamp
            };
        } catch (error) {
            await this.db.logger.error('创建备份失败', { error: error.message });
            return {
                success: false,
                error: error.message
            };
        }
    }

    async restoreFromBackup(timestamp) {
        try {
            // 先检查备份是否存在
            const backupPath = path.join(this.dataPath, 'backups');
            const backups = await fs.readdir(backupPath);
            
            // 验证时间戳格式
            if (!backups.includes(timestamp)) {
                // 尝试查找最接近的备份
                const closestBackup = backups.find(backup => 
                    backup.toLowerCase().includes(timestamp.toLowerCase())
                );
                
                if (closestBackup) {
                    timestamp = closestBackup;
                } else {
                    throw new Error(`找不到备份 ${timestamp}。可用的备份: ${backups.join(', ')}`);
                }
            }

            const backupDir = path.join(backupPath, timestamp);

            // 验证备份目录中必需的文件
            const metadataPath = path.join(backupDir, 'metadata.json');
            if (!await this.文件存在(metadataPath)) {
                throw new Error(`备份 ${timestamp} 不完整：缺少元数据文件`);
            }

            // 恢复元数据
            const metadata = JSON.parse(
                await fs.readFile(metadataPath, 'utf8')
            );

            // 验证所有表文件是否存在
            for (const tableName of Object.keys(metadata.tables)) {
                const tablePath = path.join(backupDir, `${tableName}.json`);
                if (!await this.文件存在(tablePath)) {
                    throw new Error(`备份 ${timestamp} 不完整：缺少表 ${tableName} 的数据文件`);
                }
            }

            // 清空当前数据
            this.tables.clear();
            this.indexes.clear();

            // 重置元数据，只包含备份中的表
            this.data.metadata = {
                version: metadata.version || '1.0',
                lastBackup: metadata.lastBackup,
                tables: {} // 清空当前表信息
            };

            // 恢复表数据
            for (const [tableName, tableInfo] of Object.entries(metadata.tables)) {
                try {
                    // 读取压缩的表数据
                    const compressedContent = await fs.readFile(
                        path.join(backupDir, `${tableName}.json`)
                    );
                    // 解压表数据
                    const content = await decompress(compressedContent);
                    const tableData = JSON.parse(content.toString());
                    
                    // 恢复表
                    this.tables.set(tableName, tableData);
                    
                    // 更新元数据中的表信息
                    this.data.metadata.tables[tableName] = tableInfo;

                    // 重建索引
                    if (tableData.索引) {
                        for (const indexName of tableData.索引) {
                            await this.重建索引(tableName, indexName);
                        }
                    }

                    // 重新保存表文件
                    await this.写入表文件(tableName, tableData);
                } catch (error) {
                    console.error(`恢复表 ${tableName} 失败:`, error);
                }
            }

            // 保存更新后的元数据
            await this.保存元数据();

            return {
                success: true,
                message: `数据库已恢复到备份 ${timestamp}，共恢复 ${Object.keys(metadata.tables).length} 个表`
            };
        } catch (error) {
            console.error('从备份恢复失败:', error);
            return {
                success: false,
                error: error.message
            };
        }
    }

    async cleanOldBackups() {
        const backups = await fs.readdir(this.backupPath);
        if (backups.length > this.db.config.backup.keepCount) {
            const sortedBackups = backups.sort();
            const toDelete = sortedBackups.slice(0, backups.length - this.db.config.backup.keepCount);
            
            for (const backup of toDelete) {
                await fs.rm(path.join(this.backupPath, backup), { recursive: true });
            }
        }
    }

    // 添加辅助方法
    async 文件存在(路径) {
        try {
            await fs.access(路径);
            return true;
        } catch {
            return false;
        }
    }
}

class SchemaManager {
    constructor(db) {
        this.db = db;
    }

    async addColumn(tableName, columnName, columnType, defaultValue = null) {
        const table = this.db.tables.get(tableName);
        if (!table) {
            throw new Error(`表 ${tableName} 不存在`);
        }

        table.schema[columnName] = columnType;

        if (table.storageType === 'column') {
            table.data[columnName] = new Array(Object.values(table.data)[0].length).fill(defaultValue);
        } else {
            for (const row of table.data) {
                row[columnName] = defaultValue;
            }
        }

        this.db.data.metadata.tables[tableName].schema = table.schema;

        await this.db.saveMetadata();
        if (this.db.config.storage.mode === 'separate') {
            await this.db.saveTable(tableName);
        }

        return {
            success: true,
            message: `列 ${columnName} 添加成功`
        };
    }

    async modifyColumn(tableName, columnName, newType, transformer = null) {
        const table = this.db.tables.get(tableName);
        if (!table) {
            throw new Error(`表 ${tableName} 不存在`);
        }

        if (!table.schema[columnName]) {
            throw new Error(`列 ${columnName} 不存在`);
        }

        table.schema[columnName] = newType;

        if (transformer) {
            if (table.storageType === 'column') {
                table.data[columnName] = table.data[columnName].map(transformer);
            } else {
                for (const row of table.data) {
                    row[columnName] = transformer(row[columnName]);
                }
            }
        }

        await this.db.saveMetadata();
        if (this.db.config.storage.mode === 'separate') {
            await this.db.saveTable(tableName);
        }

        return {
            success: true,
            message: `列 ${columnName} 修改成功`
        };
    }

    async dropColumn(tableName, columnName) {
        const table = this.db.tables.get(tableName);
        if (!table) {
            throw new Error(`表 ${tableName} 不存在`);
        }

        delete table.schema[columnName];

        if (table.storageType === 'column') {
            delete table.data[columnName];
        } else {
            for (const row of table.data) {
                delete row[columnName];
            }
        }

        this.db.data.metadata.tables[tableName].schema = table.schema;

        await this.db.saveMetadata();
        if (this.db.config.storage.mode === 'separate') {
            await this.db.saveTable(tableName);
        }

        return {
            success: true,
            message: `列 ${columnName} 删除成功`
        };
    }
}

class QueryCache {
    constructor(maxSize = 1000) {
        this.cache = new Map();
        this.maxSize = maxSize;
        this.hits = 0;
        this.misses = 0;
    }

    generateKey(query) {
        if (!query || typeof query !== 'object') {
            throw new Error('无效的查询参数');
        }
        return JSON.stringify({
            tableName: query.tableName,
            where: query.where || {},
            columns: query.columns || [],
            useIndex: query.useIndex || false
        });
    }

    get(query) {
        try {
            const key = this.generateKey(query);
            const cached = this.cache.get(key);
            
            if (cached && Date.now() - cached.timestamp < cached.ttl) {
                this.hits++;
                return cached.data;
            }
            
            this.misses++;
            return null;
        } catch (error) {
            console.error('缓存获取失败:', error);
            return null;
        }
    }

    set(query, data, ttl = 60000) {
        try {
            const key = this.generateKey(query);
            
            if (this.cache.size >= this.maxSize) {
                const oldestKey = Array.from(this.cache.keys())[0];
                this.cache.delete(oldestKey);
            }
            
            this.cache.set(key, {
                data,
                timestamp: Date.now(),
                ttl: Math.max(0, ttl)
            });
        } catch (error) {
            console.error('缓存设置失败:', error);
        }
    }

    clear(tableName) {
        try {
            if (tableName) {
                for (const [key, _] of this.cache.entries()) {
                    if (key.includes(tableName)) {
                        this.cache.delete(key);
                    }
                }
            } else {
                this.cache.clear();
            }
        } catch (error) {
            console.error('缓存清理失败:', error);
        }
    }

    getStats() {
        const total = this.hits + this.misses;
        return {
            size: this.cache.size,
            hits: this.hits,
            misses: this.misses,
            hitRate: total > 0 ? this.hits / total : 0
        };
    }
}

class PerformanceMonitor {
    constructor() {
        this.metrics = {
            queries: {
                count: 0,
                totalTime: 0,
                slow: []
            },
            operations: {
                insert: { count: 0, totalTime: 0 },
                update: { count: 0, totalTime: 0 },
                delete: { count: 0, totalTime: 0 },
                select: { count: 0, totalTime: 0 }
            },
            cache: {
                hits: 0,
                misses: 0
            }
        };
    }

    async measureOperation(type, operation) {
        if (!this.metrics.operations[type]) {
            console.warn(`未知的操作类型: ${type}`);
            return await operation();
        }

        const start = Date.now();
        try {
            const result = await operation();
            const duration = Date.now() - start;
            
            this.metrics.operations[type].count++;
            this.metrics.operations[type].totalTime += duration;

            if (type === 'select' && duration > 1000) {
                this.metrics.queries.slow.push({
                    timestamp: new Date(),
                    duration,
                    maxLength: 100
                });

                if (this.metrics.queries.slow.length > 100) {
                    this.metrics.queries.slow.shift();
                }
            }

            return result;
        } catch (error) {
            console.error(`操作执行失败 (${type}):`, error);
            throw error;
        }
    }

    getReport() {
        try {
            const report = {
                operations: {},
                cacheEfficiency: 0,
                slowQueries: this.metrics.queries.slow.length
            };

            for (const [type, data] of Object.entries(this.metrics.operations)) {
                report.operations[type] = {
                    count: data.count,
                    averageTime: data.count ? Math.round(data.totalTime / data.count) : 0
                };
            }

            const totalCacheRequests = this.metrics.cache.hits + this.metrics.cache.misses;
            report.cacheEfficiency = totalCacheRequests > 0 
                ? Number((this.metrics.cache.hits / totalCacheRequests).toFixed(2))
                : 0;

            return report;
        } catch (error) {
            console.error('性能报告生成失败:', error);
            return {
                error: '性能报告生成失败',
                details: error.message
            };
        }
    }

    reset() {
        this.metrics = {
            queries: {
                count: 0,
                totalTime: 0,
                slow: []
            },
            operations: {
                insert: { count: 0, totalTime: 0 },
                update: { count: 0, totalTime: 0 },
                delete: { count: 0, totalTime: 0 },
                select: { count: 0, totalTime: 0 }
            },
            cache: {
                hits: 0,
                misses: 0
            }
        };
    }
}

class DataValidator {
    constructor() {
        this.customValidators = new Map();
    }

    addValidator(name, validator) {
        if (typeof validator !== 'function') {
            throw new Error('验证器必须是函数');
        }
        this.customValidators.set(name, validator);
    }

    validate(data, schema) {
        const errors = [];
        
        for (const [field, rules] of Object.entries(schema)) {
            if (rules.required && !data.hasOwnProperty(field)) {
                errors.push(`缺少必需字段: ${field}`);
                continue;
            }

            const value = data[field];
            if (value === undefined || value === null) {
                if (rules.required) {
                    errors.push(`字段 ${field} 不能为空`);
                }
                continue;
            }

            if (rules.type && !this.checkType(value, rules.type)) {
                errors.push(`字段 ${field} 类型错误，期望 ${rules.type}`);
            }

            if (rules.min !== undefined && value < rules.min) {
                errors.push(`字段 ${field} 小于最小值 ${rules.min}`);
            }
            if (rules.max !== undefined && value > rules.max) {
                errors.push(`字段 ${field} 大于最大值 ${rules.max}`);
            }

            if (rules.minLength !== undefined && value.length < rules.minLength) {
                errors.push(`字段 ${field} 长度小于 ${rules.minLength}`);
            }
            if (rules.maxLength !== undefined && value.length > rules.maxLength) {
                errors.push(`字段 ${field} 长度大于 ${rules.maxLength}`);
            }

            if (rules.pattern && !rules.pattern.test(value)) {
                errors.push(`字段 ${field} 格式不匹配`);
            }

            if (rules.validator) {
                const validator = this.customValidators.get(rules.validator);
                if (validator && !validator(value)) {
                    errors.push(`字段 ${field} 未通过自定义验证`);
                }
            }
        }

        return {
            valid: errors.length === 0,
            errors
        };
    }

    checkType(value, type) {
        switch (type.toLowerCase()) {
            case 'string':
                return typeof value === 'string';
            case 'number':
                return typeof value === 'number' && !isNaN(value);
            case 'boolean':
                return typeof value === 'boolean';
            case 'date':
                return value instanceof Date && !isNaN(value);
            case 'array':
                return Array.isArray(value);
            case 'object':
                return typeof value === 'object' && value !== null && !Array.isArray(value);
            default:
                return true;
        }
    }
}

class Logger {
    constructor(config = {}) {
        this.config = {
            level: 'info',
            file: 'db.log',
            console: true,
            ...config
        };

        this.levels = {
            error: 0,
            warn: 1,
            info: 2,
            debug: 3
        };
        
        if (this.config.file) {
            const logDir = path.dirname(this.config.file);
            fs.mkdir(logDir, { recursive: true })
                .then(() => {
                    return fs.writeFile(this.config.file, '', { flag: 'a' });
                })
                .catch(error => {
                    if (error.code !== 'EEXIST') {
                        console.error('创建日志目录失败:', error);
                    }
                });
        }
    }
    async log(level, message, details = {}) {
        console.log("================日志记录=================");
        if (this.levels[level] > this.levels[this.config.level]) {
            return;
        }

        const logEntry = {
            timestamp: new Date().toISOString(),
            level,
            message,
            ...details
        };

        try {
            if (this.config.console) {
                console.log("================控制台输出=================");
                const color = this.getConsoleColor(level);
                console.log(`${color}[${logEntry.timestamp}] ${level.toUpperCase()}: ${message}\x1b[0m`);
                if (Object.keys(details).length > 0) {
                    console.log(details);
                }
            }

            if (this.config.file) {
                console.log("================写入日志文件=================");
                
                await this.writeToFile(logEntry);
            }
        } catch (error) {
            console.error('日志记录失败:', error);
        }
    }

    getConsoleColor(level) {
        const colors = {
            error: '\x1b[31m',
            warn: '\x1b[33m',
            info: '\x1b[36m',
            debug: '\x1b[90m'
        };
        return colors[level] || '\x1b[0m';
    }

    async writeToFile(logEntry) {
        try {
            if (!this.config.file) {
                return;
            }

            const logLine = `${JSON.stringify(logEntry)}\n`;
            await fs.appendFile(this.config.file, logLine, 'utf8');
        } catch (error) {
            console.error('写入日志失败:', error);
            try {
                const logDir = path.dirname(this.config.file);
                await fs.mkdir(logDir, { recursive: true });
                await fs.appendFile(this.config.file, logLine, 'utf8');
            } catch (retryError) {
                console.error('重试写入日志失败:', retryError);
            }
        }
    }

    async error(message, details = {}) {
        console.log("================error=================");
        await this.log('error', message, details);
    }

    async warn(message, details = {}) {
        console.log("================warn=================");
        await this.log('warn', message, details);
    }

    async info(message, details = {}) {
        console.log("================info=================");
        await this.log('info', message, details);
    }

    async debug(message, details = {}) {
        console.log("================debug=================");
        await this.log('debug', message, details);
    }
}

class CsDB {
    constructor(config) {
        // 如果已经有实例了，直接返回该实例
        if (CsDB.instance) {
            return CsDB.instance;
        }

        // 保存实例
        CsDB.instance = this;

        this.config = config;
        this.tables = new Map();
        this.lockManager = new LockManager();
        this.mvccManager = new MVCCManager();
        this.vectorProcessor = new VectorizedProcessor();
        this.queryOptimizer = new QueryOptimizer(this);
        this.dataPath = config.storage.path;
        this.indexes = new Map();
        this.transactions = new Map();
        this.transactionId = 0;
        
        // 添加元数据初始化
        this.data = {
            metadata: {
                version: '1.0',
                tables: {},
                lastBackup: null
            }
        };
        
        // 确保日志配置存在
        this.config.logger = {
            level: 'info',
            file: path.join(this.dataPath, 'db.log'),
            console: true,
            ...config.logger
        };
        
        // 初始化日志记录器
        this.logger = new Logger(this.config.logger);
        
        // 添加备份管理器
        this.backupManager = new BackupManager(this);
        
        // 初始化数据库
        this.初始化数据库();
    }

    // 添加静态获取实例的方法
    static getInstance(config) {
        if (!CsDB.instance) {
            new CsDB(config);
        }
        return CsDB.instance;
    }

    // 添加初始化数据库方法
    async 初始化数据库() {
        await this.加载元数据();
        await this.加载所有表();
    }

    async 加载元数据() {
        try {
            const 元数据路径 = path.join(this.dataPath, 'metadata.json');
            if (await this.文件存在(元数据路径)) {
                const 内容 = await fs.readFile(元数据路径);
                this.data.metadata = JSON.parse(内容.toString());
            }
        } catch (error) {
            throw new Error(`加载元数据失败: ${error.message}`);
        }
    }

    async 保存元数据() {
        try {
            const 元数据路径 = path.join(this.dataPath, 'metadata.json');
            await fs.writeFile(
                元数据路径,
                JSON.stringify(this.data.metadata, null, 2)
            );
        } catch (error) {
            throw new Error(`保存元数据失败: ${error.message}`);
        }
    }

    // 表操作
    async 创建表({ 表名, 列, 存储类型 = '行式', 列存储配置 = {} }) {
        await this.logger.info(`开始创建表 ${表名}`, { 存储类型, 列定义: 列 });
        
        try {
            if (this.tables.has(表名)) {
                throw new Error(`表 ${表名} 已存在`);
            }

            const table = {
                名称: 表名,
                列定义: 列,
                存储类型,
                列存储配置,
                数据: 存储类型 === '列式' ? 
                    Object.keys(列).reduce((acc, col) => {
                        acc[col] = [];
                        return acc;
                    }, {}) : 
                    []
            };

            // 更新元数据和内存
            this.data.metadata.tables[表名] = {
                schema: 列,
                storageType: 存储类型,
                created: new Date().toISOString()
            };
            this.tables.set(表名, table);

            // 保存到文件
            await this.写入表文件(表名, table);
            await this.保存元数据();
            await this.backupManager.createBackup();
            
            await this.logger.info(`表 ${表名} 创建成功`);
            return true;
        } catch (error) {
            await this.logger.error(`创建表 ${表名} 失败`, { error: error.message });
            throw error;
        }
    }

    async 获取表信息(表名) {
        try {
            const metadata = await this.读取元数据();
            return metadata.tables[表名];
        } catch (error) {
            throw error;
        }
    }

    async 加载表数据(表名) {
        try {
            const 表路径 = path.join(this.dataPath, `${表名}.json`);
            const 压缩内容 = await fs.readFile(表路径);
            const 内容 = await decompress(压缩内容);
            return JSON.parse(内容.toString());
        } catch (error) {
            return null;
        }
    }

    async 写入表文件(表名, 数据) {
        try {
            const 表路径 = path.join(this.dataPath, `${表名}.json`);
            const 内容 = JSON.stringify(数据, null, 2);
            const 压缩内容 = await compress(内容);
            await fs.writeFile(表路径, 压缩内容);
        } catch (error) {
            throw new Error(`写入表文件失败: ${error.message}`);
        }
    }

    // 数据操作
    async 插入数据({ 表名, 数据, 模式 = "单条" }) {
        try {
            const 表数据 = await this.加载表数据(表名);
            if (!表数据) {
                throw new Error(`表 ${表名} 不存在`);
            }

            const 插入数据 = 模式 === '批量' ? 数据 : [数据];
            await this._执行插入(表数据, 插入数据);
            await this.写入表文件(表名, 表数据);
            return true;
        } catch (error) {
            throw error;
        }
    }

    async _执行插入(表数据, 插入数据) {
        if (表数据.存储类型 === '列式') {
            for (const 行数据 of 插入数据) {
                for (const [列名, _] of Object.entries(表数据.列定义)) {
                    if (!表数据.数据[列名]) {
                        表数据.数据[列名] = [];
                    }
                    表数据.数据[列名].push(行数据[列名]);
                }
            }
        } else {
            if (!Array.isArray(表数据.数据)) {
                表数据.数据 = [];
            }
            表数据.数据.push(...插入数据);
        }
    }

    // 查询数据方法
    async 查询数据({ 表名, 列 = [], 条件 = {}, 排序 = {}, 限制 = null, 分组 = [], 聚合 = {}, 计算列 = {} }) {
        let table = await this._获取或加载表(表名);
        let results = await this._获取查询结果(table);
        
        // 先处理计算列和聚合，因为条件中可能用到这些值
        results = await this._处理计算列(results, 计算列, table);
        const { results: processedResults, aggregateValues } = await this._处理聚合(results, 聚合, 列, 限制);
        
        // 将聚合值传递给条件处理
        results = await this._处理条件(processedResults, 条件, aggregateValues);
        results = await this._处理排序(results, 排序);
        
        return { results };
    }

    // 处理聚合的独立方法
    async _处理聚合(results, 聚合, 列, 限制) {
        const aggregateValues = {};
        if (Object.keys(聚合).length > 0) {
            for (const [alias, config] of Object.entries(聚合)) {
                const { 类型, 列: targetCol, 选项 = {} } = config;
                let values;
                
                if (targetCol === '*') {
                    values = '*';
                    选项.data = results;
                } else if (targetCol in aggregateValues) {
                    values = results.map(row => row[targetCol]);
                } else {
                    values = results.map(row => row[targetCol]);
                }
                
                aggregateValues[alias] = this.vectorProcessor.compute(类型, values, 选项);
            }

            if (列.length === 1 && 列[0] in 聚合) {
                return { results: aggregateValues };
            }
        }

        // 处理列选择
        if (列.length > 0) {
            results = results.map(row => {
                const selected = {};
                列.forEach(col => {
                    if (col in aggregateValues) {
                        selected[col] = aggregateValues[col];
                    } else {
                        selected[col] = row[col];
                    }
                });
                return selected;
            });
        }

        // 处理限制
        if (限制 !== null) {
            results = results.slice(0, 限制);
        }

        return { results, aggregateValues };  // 返回聚合值和处理后的结果
    }

    async _获取或加载表(表名) {
        let table = this.tables.get(表名);
        if (!table) {
            try {
                table = await this.加载表数据(表名);
                if (!table) {
                    throw new Error(`表 ${表名} 不存在`);
                }
                this.tables.set(表名, table);
            } catch (error) {
                throw new Error(`表 ${表名} 不存在`);
            }
        }
        return table;
    }

    async _获取查询结果(table) {
        let results = [];
        if (table.存储类型 === '列式') {
            const dataLength = table.数据[Object.keys(table.数据)[0]].length;
            
            for (let i = 0; i < dataLength; i++) {
                const row = {};
                for (const [columnName, columnData] of Object.entries(table.数据)) {
                    row[columnName] = columnData[i];
                }
                results.push(row);
            }
        } else {
            results = [...table.数据];
        }
        return results;
    }

    async _处理计算列(results, 计算列, table) {
        if (Object.keys(计算列).length > 0) {
            results = results.map(row => {
                const newRow = { ...row };
                for (const [columnName, expression] of Object.entries(计算列)) {
                    try {
                        const calcFunc = new Function(...Object.keys(table.列定义), 
                            `return ${expression};`);
                        const args = Object.keys(table.列定义).map(col => 
                            Number(row[col]) || 0
                        );
                        newRow[columnName] = calcFunc(...args);
                    } catch (error) {
                        console.error(`计算列 ${columnName} 计算失败:`, error);
                        newRow[columnName] = 0;
                    }
                }
                return newRow;
            });
        }
        return results;
    }

    async _处理条件(results, 条件, aggregateValues = {}) {
        if (Object.keys(条件).length > 0) {
            results = results.filter(row => {
                return Object.entries(条件).every(([key, value]) => {
                    if (typeof value === 'object') {
                        return Object.entries(value).every(([op, val]) => {
                            if (typeof val === 'string' && val in aggregateValues) {
                                val = aggregateValues[val];
                            }
                            switch (op) {
                                case '大于': return row[key] > val;
                                case '小于': return row[key] < val;
                                case '等于': return row[key] === val;
                                case '不等于': return row[key] !== val;
                                case '包含': return row[key].includes(val);
                                default: return false;
                            }
                        });
                    }
                    return row[key] === value;
                });
            });
        }
        return results;
    }

    async _处理排序(results, 排序) {
        if (Object.keys(排序).length > 0) {
            results.sort((a, b) => {
                for (const [key, order] of Object.entries(排序)) {
                    const orderFactor = order === '降序' ? -1 : 1;
                    if (a[key] < b[key]) return -1 * orderFactor;
                    if (a[key] > b[key]) return 1 * orderFactor;
                }
                return 0;
            });
        }
        return results;
    }

    // 添加加载所有表方法
    async 加载所有表() {
        // 从元数据中获取所有表信息
        for (const [表名, _] of Object.entries(this.data.metadata.tables)) {
            if (!this.tables.has(表名)) {
                try {
                    const 表数据 = await this.加载表数据(表名);
                    if (表数据) {
                        this.tables.set(表名, 表数据);
                    }
                } catch (error) {
                    console.error(`加载表 ${表名} 失败:`, error);
                }
            }
        }
    }

    // 添加文件存在方法到 CsDB 类
    async 文件存在(路径) {
        try {
            await fs.access(路径);
            return true;
        } catch {
            return false;
        }
    }

    // 添加读取表文件方法
    async 读取表文件(表名) {
        try {
            const 表路径 = path.join(this.dataPath, `${表名}.json`);
            const 压缩内容 = await fs.readFile(表路径);
            const 内容 = await decompress(压缩内容);
            return JSON.parse(内容.toString());
        } catch (error) {
            throw new Error(`读取表 ${表名} 失败: ${error.message}`);
        }
    }

    // 添加读取元数据方法
    async 读取元数据() {
        try {
            const 元数据路径 = path.join(this.dataPath, 'metadata.json');
            const 内容 = await fs.readFile(元数据路径);
            return JSON.parse(内容.toString());
        } catch (error) {
            throw new Error(`读取元数据失败: ${error.message}`);
        }
    }

    // 添加查看表文件内容方法
    async 查看表文件内容(表名) {
        const tablePath = path.join(this.dataPath, `${表名}.json`);
        try {
            // 读取压缩文件
            const compressedContent = await fs.readFile(tablePath);
            // 解压内容
            const content = await decompress(compressedContent);
            // 解析并格式化 JSON
            const data = JSON.parse(content.toString());
            return {
                success: true,
                数据: data,
                原始大小: content.length,
                压缩大小: compressedContent.length,
                压缩比: Math.round((1 - compressedContent.length / content.length) * 100) + '%'
            };
        } catch (error) {
            return {
                success: false,
                错误: `读取表 ${表名} 失败: ${error.message}`
            };
        }
    }
}

// 清除实例的方法（主要用于测试）
CsDB.clearInstance = () => {
    CsDB.instance = null;
};

module.exports = CsDB;